<?php

namespace App\Jobs;

use App\Libraries\Tools\ConstVar;
use App\Libraries\Tools\Msg;
use App\Models\HongBao\HongBao;
use App\Models\HongBao\HongBaoTemplate;
use App\Models\Shop\Shop;
use App\Models\User\Balance;

class VerifyCouponJob extends Job
{


    private $shopCouponPurchase;


    public function __construct($shopCouponPurchase)
    {
        $this->shopCouponPurchase = $shopCouponPurchase;
    }

    /**
     * Execute the job.
     *
     * @param Shop $shop
     * @param HongBao $hongBao
     * @return bool
     */
    public function handle(Shop $shop, HongBao $hongBao)
    {
        $shop = $shop->where('shop_id', $this->shopCouponPurchase->shop_id)->firstOrFail();

        $msg = Msg::coupon_used($this->shopCouponPurchase->coupon->title);
        $flag = $hongBao->addTemplateHongBao($this->shopCouponPurchase->user_id, 'consumption', null,
            $shop->city_id, $shop->shop_id, $this->shopCouponPurchase->amount);
        if($flag){
            dispatch(new SendReceiveHongBaoMsgJob($this->shopCouponPurchase->user_id));
        }
        dispatch(new SendSystemMsgJob([$this->shopCouponPurchase->user_id], $msg, 0, ['user_id'=>$this->shopCouponPurchase->user_id, 'type'=>0]));
        return true;
    }
}
